{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [],
   "source": [
    "import time\n",
    "import utils\n",
    "from random import randint\n",
    "from bs4 import BeautifulSoup\n",
    "from selenium import webdriver"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 23,
   "metadata": {},
   "outputs": [
    {
     "ename": "NameError",
     "evalue": "name 'self' is not defined",
     "output_type": "error",
     "traceback": [
      "\u001b[1;31m---------------------------------------------------------------------------\u001b[0m",
      "\u001b[1;31mNameError\u001b[0m                                 Traceback (most recent call last)",
      "\u001b[1;32m<ipython-input-23-d4a347c1ab1a>\u001b[0m in \u001b[0;36m<module>\u001b[1;34m()\u001b[0m\n\u001b[0;32m     26\u001b[0m \u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m     27\u001b[0m \u001b[1;31m# get status_code\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[1;32m---> 28\u001b[1;33m \u001b[0mcurrent_page_url\u001b[0m \u001b[1;33m=\u001b[0m \u001b[0mself\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mdriver\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mcurrent_url\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m\u001b[0;32m     29\u001b[0m \u001b[1;32mif\u001b[0m \u001b[0mcurrent_page_url\u001b[0m \u001b[1;33m!=\u001b[0m \u001b[0mself\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0murl\u001b[0m\u001b[1;33m:\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m     30\u001b[0m     \u001b[0mprint\u001b[0m\u001b[1;33m(\u001b[0m\u001b[1;34m\"False\"\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n",
      "\u001b[1;31mNameError\u001b[0m: name 'self' is not defined"
     ]
    }
   ],
   "source": [
    "url = \"https://www.facebook.com\"\n",
    "user_name = \"2432659496@qq.com\"\n",
    "password = \"qianxuewu\"\n",
    "# options = webdriver.ChromeOptions()\n",
    "# options.set_headless()\n",
    "# options.add_argument(\"--headless\")\n",
    "# driver=webdriver.Chrome(options=options)\n",
    "driver = webdriver.Chrome()\n",
    "driver.get(url)\n",
    "assert \"Facebook\" in driver.title\n",
    "\n",
    "timeout = randint(1, 4)\n",
    "# user_name\n",
    "email_element = driver.find_element_by_id('email')\n",
    "email_element.send_keys(user_name)\n",
    "time.sleep(timeout)\n",
    "\n",
    "# password\n",
    "password_element = driver.find_element_by_id('pass')\n",
    "password_element.send_keys(password)\n",
    "time.sleep(timeout)\n",
    "\n",
    "# click\n",
    "login = driver.find_element_by_id('loginbutton')\n",
    "login.click()\n",
    "\n",
    "# get status_code\n",
    "current_page_url = self.driver.current_url\n",
    "if current_page_url != url:\n",
    "    print(\"False\")\n",
    "else:\n",
    "    print(\"True\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [],
   "source": [
    "page = driver.page_source\n",
    "soup = BeautifulSoup(page, \"html.parser\")\n",
    "li = soup.find(class_=\"clearfix sideNavItem stat_elem\")\n",
    "_id = li.get(\"data-nav-item-id\")\n",
    "homepage_url = \"https://www.facebook.com/\" + _id\n",
    "driver.get(homepage_url)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [],
   "source": [
    "def url_type_judge(_url):\n",
    "    \"\"\"\n",
    "    facebook的url分为两种：\n",
    "        1. https://www.facebook.com/erlyn.jumawan.7\n",
    "        2. https://www.facebook.com/profile.php?id=100025029671192\n",
    "    两种url的处理方式是不同的，因此需要先对其进行类型判断\n",
    "    :param _url: url\n",
    "    :return: _url_type: 1 or 2 (user_name | ID)\n",
    "    \"\"\"\n",
    "    url_root = \"https://www.facebook.com/\"\n",
    "    url_new = _url.replace(url_root, \"\")\n",
    "    url_cut = url_new.split(\".\")\n",
    "    if url_cut[0] == \"profile\":\n",
    "        _url_type = 2\n",
    "    else:\n",
    "        _url_type = 1\n",
    "\n",
    "    return _url_type"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [],
   "source": [
    "def get_jump_url(_main_page_url, _key):\n",
    "    keys = [\"photos\", \"friends\", \"videos\", \"music\", \"books\", \"tv\"]\n",
    "    if _key not in keys:\n",
    "        _url = None\n",
    "    else:\n",
    "        if url_type_judge(_main_page_url) == 1:\n",
    "            _url = url_concatenate(_main_page_url, _key)\n",
    "\n",
    "        elif url_type_judge(_main_page_url) == 2:\n",
    "            bunch = \"&sk=\" + _key\n",
    "            _url = url_concatenate(_main_page_url, bunch)\n",
    "\n",
    "        else:\n",
    "            _url = None\n",
    "\n",
    "    return _url"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {},
   "outputs": [],
   "source": [
    "def url_concatenate(base_url, join_url):\n",
    "    \"\"\"\n",
    "    链接字串合并\n",
    "    :param base_url: 主链接\n",
    "    :param join_url: 待合并字串\n",
    "    :return:\n",
    "    \"\"\"\n",
    "    if base_url[-1] == \"/\":\n",
    "        new_url = base_url + join_url\n",
    "    else:\n",
    "        new_url = base_url + \"/\" + join_url\n",
    "\n",
    "    return new_url"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {
    "scrolled": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "https://www.facebook.com/erlyn.jumawan.7\n",
      "https://www.facebook.com/profile.php?id=100025029671192\n",
      "https://www.facebook.com/md.riayd.7140\n",
      "https://www.facebook.com/profile.php?id=100022803730511\n",
      "https://www.facebook.com/tuyetnhu.bui.902\n",
      "https://www.facebook.com/nhanngocnguyen.nguyen\n",
      "https://www.facebook.com/collo.fero\n",
      "https://www.facebook.com/fain.sunar\n",
      "https://www.facebook.com/carito.gonzales.31542\n",
      "https://www.facebook.com/zahed.ahmed.9619\n",
      "https://www.facebook.com/donmasud.khan.9\n",
      "https://www.facebook.com/tachya.santos.94\n",
      "https://www.facebook.com/akash.lakade.3551\n",
      "https://www.facebook.com/mdjumon.mdjumon.315\n",
      "https://www.facebook.com/profile.php?id=100021706361596\n",
      "https://www.facebook.com/md.makh.3\n",
      "https://www.facebook.com/profile.php?id=100021582201117\n",
      "https://www.facebook.com/chahmad.kahloon.7\n",
      "https://www.facebook.com/profile.php?id=100022127991791\n",
      "https://www.facebook.com/sohel.dhali.522\n",
      "https://www.facebook.com/profile.php?id=100024513502434\n",
      "https://www.facebook.com/hajer.rs.3\n",
      "https://www.facebook.com/profile.php?id=100025101336885\n",
      "https://www.facebook.com/jubayer.khar\n",
      "https://www.facebook.com/profile.php?id=100017291017271\n",
      "https://www.facebook.com/san.sameth.3\n",
      "https://www.facebook.com/roblereymark\n",
      "https://www.facebook.com/profile.php?id=100024082658977\n",
      "https://www.facebook.com/anowar.rana.7587\n",
      "https://www.facebook.com/andria.syifa.3\n",
      "https://www.facebook.com/syed.jikrulislam.1\n",
      "https://www.facebook.com/profile.php?id=100024744552643\n",
      "https://www.facebook.com/md.parviz.750\n",
      "https://www.facebook.com/profile.php?id=100024809323483\n",
      "https://www.facebook.com/profile.php?id=100022998261927\n",
      "https://www.facebook.com/profile.php?id=100024348563827\n",
      "https://www.facebook.com/bijoy.bowmik.3\n",
      "https://www.facebook.com/nur.hohain\n",
      "https://www.facebook.com/sk.khaja.3979489\n",
      "https://www.facebook.com/profile.php?id=100013747338582\n",
      "https://www.facebook.com/profile.php?id=100024556752408\n",
      "https://www.facebook.com/profile.php?id=100024630242475\n",
      "https://www.facebook.com/profile.php?id=100004810085317\n",
      "https://www.facebook.com/jee.sila.5\n",
      "https://www.facebook.com/imran.islam.77398143\n",
      "https://www.facebook.com/ngocdeo.phungthi\n",
      "https://www.facebook.com/profile.php?id=100025015730355\n",
      "https://www.facebook.com/profile.php?id=100024536663729\n",
      "https://www.facebook.com/sapana.gautam.7528\n",
      "https://www.facebook.com/profile.php?id=100025080464279\n",
      "https://www.facebook.com/jahangeer.alam.904750\n",
      "https://www.facebook.com/md.suhidulla.92\n",
      "https://www.facebook.com/profile.php?id=100024705887203\n",
      "https://www.facebook.com/md.shabash.5\n",
      "https://www.facebook.com/profile.php?id=100022090727253\n",
      "https://www.facebook.com/musafir.mah.9\n",
      "https://www.facebook.com/profile.php?id=100024900338141\n",
      "https://www.facebook.com/profile.php?id=100023852941971\n",
      "https://www.facebook.com/mdaziz.khan.5832343\n",
      "https://www.facebook.com/vuphuong.duong.123276\n",
      "https://www.facebook.com/profile.php?id=100024555605754\n",
      "https://www.facebook.com/profile.php?id=100023234525574\n",
      "https://www.facebook.com/antnio.ramirez.925\n",
      "https://www.facebook.com/profile.php?id=100013403820779\n",
      "https://www.facebook.com/profile.php?id=100023299952982\n",
      "https://www.facebook.com/vuhai.nguyen.3979\n",
      "https://www.facebook.com/sakaeid.sobis\n",
      "https://www.facebook.com/bipin.magar.79656\n",
      "https://www.facebook.com/shakeel.salamat.31\n",
      "https://www.facebook.com/profile.php?id=100015630848126\n",
      "https://www.facebook.com/profile.php?id=100025140635815\n",
      "https://www.facebook.com/profile.php?id=100014121132564\n",
      "https://www.facebook.com/profile.php?id=100025088770763\n",
      "https://www.facebook.com/nirakar.biswal.18\n",
      "https://www.facebook.com/noy.olga.5\n",
      "https://www.facebook.com/mdnurislam.mdnurislam.3950\n",
      "https://www.facebook.com/akish.akish.925059\n",
      "https://www.facebook.com/profile.php?id=100024770280854\n",
      "https://www.facebook.com/mahmudul.hassion\n",
      "https://www.facebook.com/joselapara.alexade\n",
      "https://www.facebook.com/mdaridhosan.hosan\n",
      "https://www.facebook.com/aarsh.aarsh.9440234\n",
      "https://www.facebook.com/misba.laskar.923\n",
      "https://www.facebook.com/profile.php?id=100018034783946\n",
      "https://www.facebook.com/traingoan.kiet\n",
      "https://www.facebook.com/quays.tits.98\n",
      "https://www.facebook.com/profile.php?id=100024694254019\n",
      "https://www.facebook.com/profile.php?id=100021982218508\n",
      "https://www.facebook.com/profile.php?id=100024727131273\n",
      "https://www.facebook.com/ruman.ahmedpaglurobin\n",
      "https://www.facebook.com/profile.php?id=100013166502026\n",
      "https://www.facebook.com/profile.php?id=100023939827614\n",
      "https://www.facebook.com/mai.ngan.566790\n",
      "https://www.facebook.com/profile.php?id=100024595243587\n",
      "https://www.facebook.com/profile.php?id=100006118427873\n",
      "https://www.facebook.com/profile.php?id=100022813680506\n",
      "https://www.facebook.com/ngoc.lo.940641\n",
      "https://www.facebook.com/profile.php?id=100014271963108\n"
     ]
    }
   ],
   "source": [
    "import re\n",
    "friends_page_url = get_jump_url(homepage_url, \"friends\")\n",
    "driver.get(friends_page_url)\n",
    "page_source = driver.page_source\n",
    "soup = BeautifulSoup(page_source, \"html.parser\")\n",
    "friends_table = soup.find(class_=\"clearfix _1_ca\")\n",
    "content = friends_table.a.text\n",
    "pattern = re.compile(r\"\\d+\\.?\\d*\")\n",
    "friends_number = int(pattern.findall(content)[0])\n",
    "\n",
    "import time\n",
    "import random\n",
    "timeout = random.randint(2,5)\n",
    "for i in range(friends_number // 20):\n",
    "    driver.execute_script('window.scrollTo(0, document.body.scrollHeight);')\n",
    "    time.sleep(timeout)\n",
    "\n",
    "homepage_url_friends = list()\n",
    "count = 1\n",
    "page_source = driver.page_source\n",
    "soup = BeautifulSoup(page_source, \"html.parser\")\n",
    "\n",
    "contents = soup.find_all(class_=\"uiProfileBlockContent\")\n",
    "for content in contents:\n",
    "    homepage_url_friend = content.a.get(\"href\")\n",
    "    if utils.url_type_judge(homepage_url_friend) == 1:\n",
    "        homepage_url_friend = homepage_url_friend.replace(\"?fref=pb&hc_location=friends_tab\", \"\")\n",
    "    if utils.url_type_judge(homepage_url_friend) == 2:\n",
    "        homepage_url_friend = homepage_url_friend.replace(\"&fref=pb&hc_location=friends_tab\", \"\")\n",
    "    print(homepage_url_friend)\n",
    "    homepage_url_friends.append(homepage_url_friend)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {
    "scrolled": true
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "https://www.facebook.com/profile.php?id=100025030772822\n",
      "https://www.facebook.com/rwlmetteay\n",
      "https://www.facebook.com/profile.php?id=100015583545779\n",
      "https://www.facebook.com/xiangchaosos\n",
      "https://www.facebook.com/profile.php?id=100016373370796\n",
      "https://www.facebook.com/profile.php?id=100018549887156\n",
      "https://www.facebook.com/profile.php?id=100015767640894\n",
      "https://www.facebook.com/profile.php?id=100012652667158\n",
      "https://www.facebook.com/profile.php?id=100011264404896\n",
      "https://www.facebook.com/xia.chao.56\n",
      "https://www.facebook.com/profile.php?id=100013325323267\n",
      "https://www.facebook.com/profile.php?id=100010166383128\n",
      "https://www.facebook.com/profile.php?id=100012966280984\n",
      "https://www.facebook.com/profile.php?id=100016415492650\n"
     ]
    }
   ],
   "source": [
    "url = \"https://www.facebook.com/search/str/xiachao/keywords_users\"\n",
    "driver.get(url)\n",
    "number = 10\n",
    "times = number // 9 - 1\n",
    "\n",
    "import time\n",
    "import random\n",
    "timeout = random.randint(2,5)\n",
    "for i in range(times):\n",
    "    driver.execute_script('window.scrollTo(0, document.body.scrollHeight);')\n",
    "    time.sleep(timeout)\n",
    "\n",
    "users_link = list()\n",
    "page = driver.page_source\n",
    "soup = BeautifulSoup(page, \"html.parser\")\n",
    "users = soup.find_all(class_=\"_32mo\")\n",
    "for user in users:\n",
    "    user_link = user.get(\"href\")\n",
    "    if url_type_judge(user_link) == 1:\n",
    "        user_link = user_link.replace(\"?ref=br_rs\", \"\")\n",
    "    if url_type_judge(user_link) == 2:\n",
    "        user_link = user_link.replace(\"&ref=br_rs\", \"\")\n",
    "    print(user_link)\n",
    "    users_link.append(user_link)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "通知\n",
      "通知\n",
      "通知\n",
      "通知\n",
      "通知\n",
      "通知\n",
      "通知\n",
      "Taylor Makenzie 的更多信息\n"
     ]
    }
   ],
   "source": [
    "# url = \"https://www.facebook.com/profile.php?id=100025148614740&sk=photos\"\n",
    "url = \"https://www.facebook.com/taylor.makenzie.79/photos\"\n",
    "driver.get(url)\n",
    "page = driver.page_source\n",
    "soup = BeautifulSoup(page, \"html.parser\")\n",
    "bottem = soup.find_all(class_=\"uiHeaderTitle\")\n",
    "\n",
    "import time\n",
    "import random\n",
    "timeout = random.randint(2,5)\n",
    "while \"更多信息\" not in bottem[-1].string or \"More about\" not in bottem[-1].string:\n",
    "    driver.execute_script(\"window.scrollTo(0, document.body.scrollHeight);\")\n",
    "    time.sleep(timeout)\n",
    "    \n",
    "    page = driver.page_source\n",
    "    soup = BeautifulSoup(page, \"html.parser\")\n",
    "    bottem = soup.find_all(class_=\"uiHeaderTitle\")\n",
    "    print(bottem[-1].string)\n",
    "    if \"更多信息\" in bottem[-1].string or \"More about\" in bottem[-1].string:\n",
    "        break"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "metadata": {
    "scrolled": true
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "link: https://scontent-nrt1-1.xx.fbcdn.net/v/t1.0-9/29790693_110531536461800_8350038227599897795_n.jpg?_nc_cat=0&oh=fc7a08ea00a8a5c08e9f435f1bf08a55&oe=5B292D2E\n",
      "date: 1522391519\n",
      "text: []\n",
      "location: []\n"
     ]
    }
   ],
   "source": [
    "url = \"https://www.facebook.com/photo.php?fbid=110531536461800&set=pb.100025148614740.-2207520000.1522845380.&type=3&theater\"\n",
    "driver.get(url)\n",
    "page = driver.page_source\n",
    "soup = BeautifulSoup(page, \"html.parser\")\n",
    "spotlight = soup.find(class_=\"spotlight\")\n",
    "link = spotlight.get(\"src\")\n",
    "\n",
    "publish_time = soup.find(\"span\", {\"id\": \"fbPhotoSnowliftTimestamp\"})\n",
    "date = publish_time.a.abbr.get(\"data-utime\")\n",
    "location_object = soup.find(class_=\"fbPhotosImplicitLocLink\")\n",
    "if location_object is not None:\n",
    "    location = location_object.text\n",
    "else:\n",
    "    location = []\n",
    "\n",
    "text_object = soup.find(\"span\", {\"class\": \"hasCaption\"})\n",
    "if text_object is not None:\n",
    "    text = text_object.text\n",
    "else:\n",
    "    text = []\n",
    "print(\"link:\", link)\n",
    "print(\"date:\", date)\n",
    "print(\"text:\", text)\n",
    "print(\"location:\", location)\n",
    "\n",
    "import shutil\n",
    "import requests\n",
    "response = requests.get(link, stream = True)\n",
    "with open(\"test.jpg\", \"wb\") as file:\n",
    "#     file.write(response.raw)\n",
    "    shutil.copyfileobj(response.raw, file)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "29683314_110531449795142_3054763241779463875_n\n"
     ]
    }
   ],
   "source": [
    "url = \"https://scontent-nrt1-1.xx.fbcdn.net/v/t1.0-9/29683314_110531449795142_3054763241779463875_n.jpg?_nc_cat=0&oh=df15ea09f14c6905945250c8e7b67aa3&oe=5B6D3EF2\"\n",
    "print(((url.split(\"/\")[-1]).split(\"?\")[0]).split(\".\")[0])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 18,
   "metadata": {},
   "outputs": [],
   "source": [
    "url = \"https://www.facebook.com/profile.php?id=100025148614740\"\n",
    "# driver.get(url)\n",
    "post_element = driver.find_element_by_class_name(\"_3jk\")\n",
    "post_element.click()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 31,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "headless\n",
      "head\n"
     ]
    }
   ],
   "source": [
    "options=webdriver.ChromeOptions()\n",
    "options.set_headless()\n",
    "options.add_argument(\"--disable-gpu\")\n",
    "driver=webdriver.Chrome(options=options)\n",
    "driver.get(\"http://www.baidu.com\")\n",
    "print(\"headless\")\n",
    "\n",
    "options=webdriver.ChromeOptions()\n",
    "driver=webdriver.Chrome(options=options)\n",
    "driver.get(\"http://www.baidu.com\")\n",
    "print(\"head\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 95,
   "metadata": {},
   "outputs": [],
   "source": [
    "#!/usr/bin/env python\n",
    "# -*- coding: utf-8 -*-\n",
    "\"\"\"\n",
    "Created on 2018.03.09\n",
    "Finished on \n",
    "@author: Wang Yuntao\n",
    "\"\"\"\n",
    "\n",
    "import re\n",
    "import time\n",
    "import utils\n",
    "from crawl import *\n",
    "from random import randint\n",
    "from bs4 import BeautifulSoup\n",
    "from selenium import webdriver\n",
    "\n",
    "\"\"\"\n",
    "    function:\n",
    "        __init__(self, _user_name=None, _password=None, \n",
    "                  _browser_type=\"Chrome\", is_headless=False)        __init__\n",
    "        log_in(self)                                                Facebook登录\n",
    "        make_post(self)                                             发布状态\n",
    "        page_refresh(self, _refresh_times=0)                        页面下拉刷新\n",
    "        enter_homepage_self(self)                                   进入当前账户的个人主页 (方便对用户好友和照片的获取)\n",
    "        get_user_id(self, _user_homepage_url)                       获取用户id\n",
    "        get_friends_list(self, _friends_number=None)                获取当前账户的好友列表 (列表存储各好友的主页url)\n",
    "        search_users(self, _keyword, user_number)                   获取当前搜索条件下的用户列表 (列表存储各用户的主页url)\n",
    "        get_photos_list(self)                                       获取照片的href，方便对原图的链接，发表时间等进行获取\n",
    "        get_photo_info(self, _photo_href)                           获取照片的链接，发布时间，发布位置，尺寸与对应的文字说明\n",
    "        get_photos_info_list(self, _photos_href_list)               批量获取照片的链接，发布时间，发布位置，尺寸与对应的文字说明\n",
    "        download_photos_one(self, _homepage_url)                    下载单个用户的图片\n",
    "        download_photos_batch(self, _homepage_url_list)             批量下载多个用户的图片\n",
    "\"\"\"\n",
    "\n",
    "\n",
    "class Facebook:\n",
    "    def __init__(self, _user_name=None, _password=None, _browser_type=\"Chrome\", _is_headless=False):\n",
    "        # the variables which are fixed\n",
    "        self.url = \"https://www.facebook.com/\"                      # facebook页面url\n",
    "        self.user_name = _user_name                                 # 帐户名\n",
    "        self.password = _password                                   # 密码\n",
    "        self.browser_state = None                                   # 浏览器选择状态\n",
    "        self.login_state = None                                     # 登录状态\n",
    "        self.homepage_url = None                                    # 当前登录账号的主页url\n",
    "\n",
    "        # some parameters of webdriver\n",
    "        self.cookie = None                                          # 当前登录账号的cookie\n",
    "        self.session_id = None                                      # 会话id，方便在当前打开窗口继续运行\n",
    "        self.executor_url = None                                    # 会话的命令执行器连接\n",
    "        self.cookies = None                                         # 用户cookies\n",
    "\n",
    "        # the initialization of list\n",
    "        self.homepage_url_friends = list()                          # 好友主页url列表\n",
    "        self.homepage_url_users = list()                            # 通过搜索得到的用户url列表\n",
    "\n",
    "        # the variables which are variant regularly\n",
    "        self.id_class_name = \"clearfix sideNavItem stat_elem\"       # 从www.facebook.com页面获取用户id所需class name\n",
    "        self.post_class_name = \"_3jk\"                               # 状态发布所需class name\n",
    "        self.search_class_name = \"_32mo\"                            # 用户搜索所需class name\n",
    "        self.friends_class_name = \"clearfix _1_ca\"                  # 好友列表获取所需class name\n",
    "\n",
    "        # the selection of browser\n",
    "        if _browser_type == \"Chrome\":\n",
    "            try:\n",
    "                options = webdriver.ChromeOptions()\n",
    "                if _is_headless is True:\n",
    "                    options.set_headless()\n",
    "                    options.add_argument(\"--disable - gpu\")\n",
    "                self.driver = webdriver.Chrome(options=options)\n",
    "                self.browser_state = 1\n",
    "            except AttributeError:\n",
    "                self.browser_state = 0\n",
    "\n",
    "        if _browser_type == \"Firefox\":\n",
    "            try:\n",
    "                options = webdriver.FirefoxOptions()\n",
    "                if _is_headless is True:\n",
    "                    options.set_headless()\n",
    "                    options.add_argument(\"--disable - gpu\")\n",
    "                self.driver = webdriver.Firefox(options=options)\n",
    "                self.browser_state = 1\n",
    "            except AttributeError:\n",
    "                self.browser_state = 0\n",
    "\n",
    "        if self.browser_state == 1:\n",
    "            self.session_id = self.driver.session_id\n",
    "            self.executor_url = self.driver.command_executor\n",
    "\n",
    "    def log_in(self):\n",
    "        \"\"\"\n",
    "        facebook log in via webdriver\n",
    "        :return: a status code —— True: Success, False: False\n",
    "        Note:\n",
    "            如果facebook账号登录成功，则当前页面的url为:https://www.facebook.com\n",
    "            如果facebook账号登录失败，则当前页面的url为:https://www.facebook.com/login.php?login_attempt=1&lwv=100\n",
    "        \"\"\"\n",
    "        timeout = randint(1, 4)\n",
    "        self.driver.get(self.url)\n",
    "\n",
    "        # username\n",
    "        email_element = self.driver.find_element_by_id('email')\n",
    "        email_element.clear()\n",
    "        email_element.send_keys(self.user_name)\n",
    "        time.sleep(timeout)\n",
    "\n",
    "        # password\n",
    "        password_element = self.driver.find_element_by_id('pass')\n",
    "        password_element.clear()\n",
    "        password_element.send_keys(self.password)\n",
    "        time.sleep(timeout)\n",
    "\n",
    "        # click\n",
    "        login = self.driver.find_element_by_id('loginbutton')\n",
    "        login.click()\n",
    "\n",
    "        self.cookies = self.driver.get_cookies()\n",
    "\n",
    "        # status judgement\n",
    "        current_page_url = self.driver.current_url\n",
    "        if current_page_url != self.url:\n",
    "            self.login_state = 0\n",
    "        else:\n",
    "            self.login_state = 1\n",
    "\n",
    "    def make_post(self):\n",
    "        self.enter_homepage_self()\n",
    "        post_element = self.driver.find_element_by_class_name(self.post_class_name)\n",
    "        post_element.click()\n",
    "\n",
    "    def page_refresh(self, _refresh_times=0):\n",
    "        timeout = randint(2, 8)\n",
    "        for i in range(_refresh_times):\n",
    "            self.driver.execute_script('window.scrollTo(0, document.body.scrollHeight);')\n",
    "            time.sleep(timeout)\n",
    "\n",
    "    def enter_homepage_self(self):\n",
    "        \"\"\"\n",
    "        进入个人主页，facebook登录后页面仍停留在https://www.facebook.com，需要进一步跳转到个人主页，获取到主页url，\n",
    "        方便对好友列表，照片的获取\n",
    "        :return:\n",
    "        \"\"\"\n",
    "        page_source = self.driver.page_source\n",
    "        soup = BeautifulSoup(page_source, \"html.parser\")\n",
    "        li = soup.find(class_=self.id_class_name)\n",
    "        user_id = li.get(\"data-nav-item-id\")\n",
    "        self.homepage_url = utils.url_concatenate(self.url, user_id)\n",
    "        self.driver.get(self.homepage_url)\n",
    "\n",
    "    def get_user_id(self, _user_homepage_url):\n",
    "        if utils.url_type_judge(_user_homepage_url) == 1:\n",
    "            self.driver.get(_user_homepage_url)\n",
    "            page = self.driver.page_source\n",
    "            soup = BeautifulSoup(page, \"html.parser\")\n",
    "            href = soup.find(class_=\"photoContainer\")\n",
    "            _user_id = href.a.get(\"href\").split(\"id=\")[-1]\n",
    "        else:\n",
    "            _user_id = _user_homepage_url.split(\"id=\")[-1]\n",
    "\n",
    "        return _user_id\n",
    "\n",
    "    def get_friends_list(self, _friends_number=None):\n",
    "        \"\"\"\n",
    "        获取好友列表，粗略估计每次刷新新增20个用户信息\n",
    "        :param _friends_number:\n",
    "        :return:\n",
    "        \"\"\"\n",
    "        friends_page_url = utils.get_jump_url(self.homepage_url, \"friends\")\n",
    "        self.driver.get(friends_page_url)\n",
    "        page_source = self.driver.page_source\n",
    "        soup = BeautifulSoup(page_source, \"html.parser\")\n",
    "\n",
    "        # 获取好友数量\n",
    "        friends_table = soup.find(class_=self.friends_class_name)              # class name可能会变\n",
    "        content = friends_table.a.text\n",
    "        pattern = re.compile(r\"\\d+\\.?\\d*\")\n",
    "        friends_number = int(pattern.findall(content)[0])\n",
    "\n",
    "        # 根据好友数量进行页面刷新\n",
    "        refresh_times = friends_number // 20\n",
    "        self.page_refresh(refresh_times)\n",
    "\n",
    "        # 重新对刷新后的页面进行解析\n",
    "        page_source = self.driver.page_source\n",
    "        soup = BeautifulSoup(page_source, \"html.parser\")\n",
    "\n",
    "        # 获取好友url列表\n",
    "        contents = soup.find_all(class_=\"uiProfileBlockContent\")\n",
    "        for content in contents:\n",
    "            homepage_url_friend = content.a.get(\"href\")\n",
    "            if utils.url_type_judge(homepage_url_friend) == 1:\n",
    "                homepage_url_friend = homepage_url_friend.replace(\"?fref=pb&hc_location=friends_tab\", \"\")\n",
    "            if utils.url_type_judge(homepage_url_friend) == 2:\n",
    "                homepage_url_friend = homepage_url_friend.replace(\"&fref=pb&hc_location=friends_tab\", \"\")\n",
    "            self.homepage_url_friends.append(homepage_url_friend)\n",
    "\n",
    "    def search_users(self, _keyword, user_number):\n",
    "        search_url = \"https://www.facebook.com/search/str/\" + _keyword + \"keywords_users\"\n",
    "        self.driver.get(search_url)\n",
    "        refresh_times = user_number // 6\n",
    "        self.page_refresh(refresh_times)\n",
    "        page_source = self.driver.page_source\n",
    "        soup = BeautifulSoup(page_source, \"html.parser\")\n",
    "\n",
    "        # 获取user homepage列表\n",
    "        users_info = soup.find_all(class_=self.search_class_name)\n",
    "        for user in users_info:\n",
    "            user_link = user.get(\"href\")\n",
    "            if utils.url_type_judge(user_link) == 1:\n",
    "                user_link = user_link.replace(\"?ref=br_rs\", \"\")\n",
    "            if utils.url_type_judge(user_link) == 2:\n",
    "                user_link = user_link.replace(\"&ref=br_rs\", \"\")\n",
    "            print(user_link)\n",
    "            self.homepage_url_users.append(user_link)\n",
    "\n",
    "    def get_photos_list(self, _homepage_url):\n",
    "        photos_url = utils.get_jump_url(_homepage_url, \"photos\")\n",
    "        self.driver.get(photos_url)\n",
    "        page = self.driver.page_source\n",
    "        soup = BeautifulSoup(page)\n",
    "        bottom_flag = soup.find_all(class_=\"uiHeaderTitle\")\n",
    "\n",
    "        timeout = random.randint(2, 5)\n",
    "        photos_link_list = list()\n",
    "        while \"更多信息\" not in bottom_flag[-1].string or \"More about\" not in bottom_flag[-1].string:\n",
    "            self.driver.execute_script(\"window.scrollTo(0, document.body.scrollHeight);\")\n",
    "            time.sleep(timeout)\n",
    "\n",
    "            page = self.driver.page_source\n",
    "            soup = BeautifulSoup(page, \"html.parser\")\n",
    "            bottom_flag = soup.find_all(class_=\"uiHeaderTitle\")\n",
    "            print(bottom_flag[-1].string)\n",
    "            if \"更多信息\" in bottom_flag[-1].string or \"More about\" in bottom_flag[-1].string:\n",
    "                break\n",
    "\n",
    "        for data in soup.find_all(class_=\"uiMediaThumb _6i9 uiMediaThumbMedium\"):\n",
    "            photos_link_list.append(data.get(\"href\"))\n",
    "\n",
    "        return photos_link_list\n",
    "\n",
    "    def get_photo_info(self, _photo_href):\n",
    "        self.driver.get(_photo_href)\n",
    "        page = self.driver.page_source\n",
    "        soup = BeautifulSoup(page)\n",
    "\n",
    "        publish_time = soup.find(\"span\", {\"id\": \"fbPhotoSnowliftTimestamp\"})\n",
    "        _date = publish_time.a.abbr.get(\"data-utime\")                           # 图片发表的时间 (Unix时间戳)\n",
    "        location_object = soup.find(class_=\"fbPhotosImplicitLocLink\")           # 图片发表的位置信息\n",
    "        if location_object is not None:\n",
    "            _location = location_object.text\n",
    "        else:\n",
    "            _location = []\n",
    "\n",
    "        text_object = soup.find(\"span\", {\"class\": \"hasCaption\"})                # 图片发表时对应的文字说明\n",
    "        if text_object is not None:\n",
    "            _text = text_object.text\n",
    "        else:\n",
    "            _text = []\n",
    "\n",
    "        # 进入全屏状态\n",
    "        full_screen_element = self.driver.find_element_by_id(\"fbPhotoSnowliftFullScreenSwitch\")\n",
    "        full_screen_element.click()\n",
    "\n",
    "        spotlight = soup.find_all(class_=\"spotlight\")\n",
    "        _link = spotlight.get(\"href\")                                           # 图片链接\n",
    "        style = spotlight.get(\"style\")                                          # 图片尺寸字符串\n",
    "        _width, _height = utils.get_size(style)                                 # 获取图像的宽和高\n",
    "\n",
    "        return _link, _date, _location, _text, _width, _height\n",
    "\n",
    "    def get_photos_info_list(self, _photos_href_list):\n",
    "        timeout = randint(2, 8)\n",
    "        _photos_info_list = list()\n",
    "        for photo_href in _photos_href_list:\n",
    "            link, date, location, text, width, height = self.get_photo_info(photo_href)\n",
    "            _photos_info_list.append([link, date, location, text, width, height])\n",
    "            time.sleep(timeout)\n",
    "\n",
    "        return _photos_info_list\n",
    "\n",
    "    def download_photos_one(self, _homepage_url):\n",
    "        photos_href_myself = self.get_photos_list(_homepage_url)\n",
    "        photos_info_list = self.get_photos_info_list(photos_href_myself)\n",
    "        for photo_info in photos_info_list:\n",
    "            utils.download(photo_info[0])\n",
    "\n",
    "    def download_photos_batch(self, _homepage_url_list):\n",
    "        for _homepage_url in _homepage_url_list:\n",
    "            photos_href_myself = self.get_photos_list(_homepage_url)\n",
    "            photos_info_list = self.get_photos_info_list(photos_href_myself)\n",
    "            print(photos_info_list)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 106,
   "metadata": {},
   "outputs": [],
   "source": [
    "email = \"2432659496@qq.com\"\n",
    "password = \"qianxuewu\"\n",
    "fb = Facebook(email, password, \"Chrome\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 107,
   "metadata": {},
   "outputs": [],
   "source": [
    "fb.log_in()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 108,
   "metadata": {},
   "outputs": [],
   "source": [
    "fb.enter_homepage_self()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 111,
   "metadata": {},
   "outputs": [],
   "source": [
    "fb.driver.get(\"https://www.facebook.com\")\n",
    "fb.make_post()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 112,
   "metadata": {},
   "outputs": [],
   "source": [
    "fb.get_friends_list()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 120,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "No.1 https://www.facebook.com/erlyn.jumawan.7\n",
      "No.2 https://www.facebook.com/profile.php?id=100025029671192\n",
      "No.3 https://www.facebook.com/md.riayd.7140\n",
      "No.4 https://www.facebook.com/profile.php?id=100022803730511\n",
      "No.5 https://www.facebook.com/tuyetnhu.bui.902\n",
      "No.6 https://www.facebook.com/nhanngocnguyen.nguyen\n",
      "No.7 https://www.facebook.com/collo.fero\n",
      "No.8 https://www.facebook.com/fain.sunar\n",
      "No.9 https://www.facebook.com/zahed.ahmed.9619\n",
      "No.10 https://www.facebook.com/donmasud.khan.9\n",
      "No.11 https://www.facebook.com/tachya.santos.94\n",
      "No.12 https://www.facebook.com/akash.lakade.3551\n",
      "No.13 https://www.facebook.com/mdjumon.mdjumon.315\n",
      "No.14 https://www.facebook.com/profile.php?id=100021706361596\n",
      "No.15 https://www.facebook.com/md.makh.3\n",
      "No.16 https://www.facebook.com/profile.php?id=100021582201117\n",
      "No.17 https://www.facebook.com/chahmad.kahloon.7\n",
      "No.18 https://www.facebook.com/profile.php?id=100022127991791\n",
      "No.19 https://www.facebook.com/sohel.dhali.522\n",
      "No.20 https://www.facebook.com/profile.php?id=100024513502434\n",
      "No.21 https://www.facebook.com/hajer.rs.3\n",
      "No.22 https://www.facebook.com/profile.php?id=100025101336885\n",
      "No.23 https://www.facebook.com/jubayer.khar\n",
      "No.24 https://www.facebook.com/profile.php?id=100017291017271\n",
      "No.25 https://www.facebook.com/san.sameth.3\n",
      "No.26 https://www.facebook.com/roblereymark\n",
      "No.27 https://www.facebook.com/profile.php?id=100024082658977\n",
      "No.28 https://www.facebook.com/anowar.rana.7587\n",
      "No.29 https://www.facebook.com/andria.syifa.3\n",
      "No.30 https://www.facebook.com/syed.jikrulislam.1\n",
      "No.31 https://www.facebook.com/profile.php?id=100024744552643\n",
      "No.32 https://www.facebook.com/md.parviz.750\n",
      "No.33 https://www.facebook.com/profile.php?id=100024809323483\n",
      "No.34 https://www.facebook.com/profile.php?id=100022998261927\n",
      "No.35 https://www.facebook.com/profile.php?id=100024348563827\n",
      "No.36 https://www.facebook.com/bijoy.bowmik.3\n",
      "No.37 https://www.facebook.com/nur.hohain\n",
      "No.38 https://www.facebook.com/sk.khaja.3979489\n",
      "No.39 https://www.facebook.com/profile.php?id=100013747338582\n",
      "No.40 https://www.facebook.com/profile.php?id=100024556752408\n",
      "No.41 https://www.facebook.com/profile.php?id=100024630242475\n",
      "No.42 https://www.facebook.com/profile.php?id=100004810085317\n",
      "No.43 https://www.facebook.com/jee.sila.5\n",
      "No.44 https://www.facebook.com/imran.islam.77398143\n",
      "No.45 https://www.facebook.com/ngocdeo.phungthi\n",
      "No.46 https://www.facebook.com/profile.php?id=100025015730355\n",
      "No.47 https://www.facebook.com/profile.php?id=100024536663729\n",
      "No.48 https://www.facebook.com/sapana.gautam.7528\n",
      "No.49 https://www.facebook.com/profile.php?id=100025080464279\n",
      "No.50 https://www.facebook.com/jahangeer.alam.904750\n",
      "No.51 https://www.facebook.com/md.suhidulla.92\n",
      "No.52 https://www.facebook.com/profile.php?id=100024705887203\n",
      "No.53 https://www.facebook.com/md.shabash.5\n",
      "No.54 https://www.facebook.com/profile.php?id=100022090727253\n",
      "No.55 https://www.facebook.com/musafir.mah.9\n",
      "No.56 https://www.facebook.com/profile.php?id=100024900338141\n",
      "No.57 https://www.facebook.com/profile.php?id=100023852941971\n",
      "No.58 https://www.facebook.com/mdaziz.khan.5832343\n",
      "No.59 https://www.facebook.com/vuphuong.duong.123276\n",
      "No.60 https://www.facebook.com/profile.php?id=100024555605754\n",
      "No.61 https://www.facebook.com/profile.php?id=100023234525574\n",
      "No.62 https://www.facebook.com/antnio.ramirez.925\n",
      "No.63 https://www.facebook.com/profile.php?id=100013403820779\n",
      "No.64 https://www.facebook.com/profile.php?id=100023299952982\n",
      "No.65 https://www.facebook.com/vuhai.nguyen.3979\n",
      "No.66 https://www.facebook.com/sakaeid.sobis\n",
      "No.67 https://www.facebook.com/bipin.magar.79656\n",
      "No.68 https://www.facebook.com/shakeel.salamat.31\n",
      "No.69 https://www.facebook.com/profile.php?id=100015630848126\n",
      "No.70 https://www.facebook.com/profile.php?id=100025140635815\n",
      "No.71 https://www.facebook.com/profile.php?id=100014121132564\n",
      "No.72 https://www.facebook.com/profile.php?id=100025088770763\n",
      "No.73 https://www.facebook.com/nirakar.biswal.18\n",
      "No.74 https://www.facebook.com/noy.olga.5\n",
      "No.75 https://www.facebook.com/mdnurislam.mdnurislam.3950\n",
      "No.76 https://www.facebook.com/akish.akish.925059\n",
      "No.77 https://www.facebook.com/profile.php?id=100024770280854\n",
      "No.78 https://www.facebook.com/mahmudul.hassion\n",
      "No.79 https://www.facebook.com/joselapara.alexade\n",
      "No.80 https://www.facebook.com/mdaridhosan.hosan\n",
      "No.81 https://www.facebook.com/aarsh.aarsh.9440234\n",
      "No.82 https://www.facebook.com/misba.laskar.923\n",
      "No.83 https://www.facebook.com/profile.php?id=100018034783946\n",
      "No.84 https://www.facebook.com/traingoan.kiet\n",
      "No.85 https://www.facebook.com/quays.tits.98\n",
      "No.86 https://www.facebook.com/profile.php?id=100024694254019\n",
      "No.87 https://www.facebook.com/profile.php?id=100021982218508\n",
      "No.88 https://www.facebook.com/profile.php?id=100024727131273\n",
      "No.89 https://www.facebook.com/ruman.ahmedpaglurobin\n",
      "No.90 https://www.facebook.com/profile.php?id=100013166502026\n",
      "No.91 https://www.facebook.com/profile.php?id=100023939827614\n",
      "No.92 https://www.facebook.com/mai.ngan.566790\n",
      "No.93 https://www.facebook.com/profile.php?id=100024595243587\n",
      "No.94 https://www.facebook.com/profile.php?id=100006118427873\n",
      "No.95 https://www.facebook.com/profile.php?id=100022813680506\n",
      "No.96 https://www.facebook.com/ngoc.lo.940641\n",
      "No.97 https://www.facebook.com/profile.php?id=100014271963108\n"
     ]
    }
   ],
   "source": [
    "number = 1\n",
    "for homepage in fb.homepage_url_friends:\n",
    "    print(\"No.%d %s\" % (number, homepage))\n",
    "    number += 1"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 124,
   "metadata": {},
   "outputs": [],
   "source": [
    "def get_jump_url(_main_page_url, _key):\n",
    "    keys = [\"photos\", \"friends\", \"videos\", \"music\", \"books\", \"tv\"]\n",
    "    if _key not in keys:\n",
    "        _url = None\n",
    "    else:\n",
    "        if url_type_judge(_main_page_url) == 1:\n",
    "            _url = url_concatenate(_main_page_url, _key)\n",
    "\n",
    "        elif url_type_judge(_main_page_url) == 2:\n",
    "            bunch = \"&sk=\" + _key\n",
    "            _url = url_concatenate(_main_page_url, bunch)\n",
    "\n",
    "        else:\n",
    "            _url = None\n",
    "\n",
    "    return _url"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 135,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "更多关于你的信息\n"
     ]
    }
   ],
   "source": [
    "photos_url = get_jump_url(fb.homepage_url, \"photos\")\n",
    "fb.driver.get(photos_url)\n",
    "page = fb.driver.page_source\n",
    "soup = BeautifulSoup(page, \"html.parser\")\n",
    "bottom_flag = soup.find_all(class_=\"uiHeaderTitle\")\n",
    "\n",
    "timeout = random.randint(2, 5)\n",
    "photos_link_list = list()\n",
    "while \"更多\" not in bottom_flag[-1].string or \"More about\" not in bottom_flag[-1].string:\n",
    "    fb.driver.execute_script(\"window.scrollTo(0, document.body.scrollHeight);\")\n",
    "    time.sleep(timeout)\n",
    "\n",
    "    page = fb.driver.page_source\n",
    "    soup = BeautifulSoup(page, \"html.parser\")\n",
    "    bottom_flag = soup.find_all(class_=\"uiHeaderTitle\")\n",
    "    print(bottom_flag[-1].string)\n",
    "    if \"更多\" in bottom_flag[-1].string or \"More about\" in bottom_flag[-1].string:\n",
    "        break\n",
    "\n",
    "for data in soup.find_all(class_=\"uiMediaThumb _6i9 uiMediaThumbMedium\"):\n",
    "    photos_link_list.append(data.get(\"href\"))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 130,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "https://www.facebook.com/photo.php?fbid=126169088231378&set=pb.100025148614740.-2207520000.1523029435.&type=3&size=960%2C632\n",
      "https://www.facebook.com/photo.php?fbid=110531536461800&set=pb.100025148614740.-2207520000.1523029435.&type=3&size=1200%2C791\n",
      "https://www.facebook.com/photo.php?fbid=110531466461807&set=pb.100025148614740.-2207520000.1523029435.&type=3&size=1200%2C675\n",
      "https://www.facebook.com/photo.php?fbid=110531449795142&set=pb.100025148614740.-2207520000.1523029435.&type=3&size=1200%2C675\n",
      "https://www.facebook.com/photo.php?fbid=110531433128477&set=pb.100025148614740.-2207520000.1523029435.&type=3&size=1200%2C750\n",
      "https://www.facebook.com/photo.php?fbid=108757339972553&set=pb.100025148614740.-2207520000.1523029435.&type=3&size=1800%2C1200\n",
      "https://www.facebook.com/photo.php?fbid=103087587206195&set=pb.100025148614740.-2207520000.1523029435.&type=3&size=1200%2C750\n",
      "https://www.facebook.com/photo.php?fbid=103087207206233&set=pb.100025148614740.-2207520000.1523029435.&type=3&size=606%2C606\n"
     ]
    }
   ],
   "source": [
    "for photo_link in photos_link_list:\n",
    "    print(photo_link)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 140,
   "metadata": {},
   "outputs": [],
   "source": [
    "def get_size(_style):\n",
    "    pattern = re.compile(r\"\\d+\\.?\\d*\")\n",
    "    content = pattern.findall(_style)\n",
    "    _width, _height = content[0], content[1]\n",
    "\n",
    "    return _width, _height"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 144,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "https://scontent-nrt1-1.xx.fbcdn.net/v/t1.0-9/29793014_126169091564711_4485160203192369152_n.jpg?_nc_cat=0&oh=bf6bfad4cd74754c26892401deec761c&oe=5B6AC97B 1522980982 [] 这样应该可以吧 960 632\n"
     ]
    }
   ],
   "source": [
    "# fb.driver.get(\"https://www.facebook.com/photo.php?fbid=126169088231378&set=pb.100025148614740.-2207520000.1523029435.&type=3&theater\")\n",
    "page = fb.driver.page_source\n",
    "soup = BeautifulSoup(page, \"html.parser\")\n",
    "\n",
    "publish_time = soup.find(\"span\", {\"id\": \"fbPhotoSnowliftTimestamp\"})\n",
    "_date = publish_time.a.abbr.get(\"data-utime\")                           # 图片发表的时间 (Unix时间戳)\n",
    "location_object = soup.find(class_=\"fbPhotosImplicitLocLink\")           # 图片发表的位置信息\n",
    "if location_object is not None:\n",
    "    _location = location_object.text\n",
    "else:\n",
    "    _location = []\n",
    "\n",
    "text_object = soup.find(\"span\", {\"class\": \"hasCaption\"})                # 图片发表时对应的文字说明\n",
    "if text_object is not None:\n",
    "    _text = text_object.text\n",
    "else:\n",
    "    _text = []\n",
    "\n",
    "# 进入全屏状态\n",
    "full_screen_element = fb.driver.find_element_by_id(\"fbPhotoSnowliftFullScreenSwitch\")\n",
    "full_screen_element.click()\n",
    "page = fb.driver.page_source\n",
    "soup = BeautifulSoup(page, \"html.parser\")\n",
    "\n",
    "spotlight = soup.find(class_=\"spotlight\")\n",
    "_link = spotlight.get(\"src\")                                            # 图片链接\n",
    "style = spotlight.get(\"style\")                                          # 图片尺寸字符串\n",
    "_width, _height = get_size(style)                                 # 获取图像的宽和高\n",
    "\n",
    "print(_link, _date, _location, _text, _width, _height)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 146,
   "metadata": {
    "scrolled": true
   },
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "c:\\program files\\python35\\lib\\site-packages\\bs4\\__init__.py:181: UserWarning: No parser was explicitly specified, so I'm using the best available HTML parser for this system (\"lxml\"). This usually isn't a problem, but if you run this code on another system, or in a different virtual environment, it may use a different parser and behave differently.\n",
      "\n",
      "The code that caused this warning is on line 193 of the file c:\\program files\\python35\\lib\\runpy.py. To get rid of this warning, change code that looks like this:\n",
      "\n",
      " BeautifulSoup(YOUR_MARKUP})\n",
      "\n",
      "to this:\n",
      "\n",
      " BeautifulSoup(YOUR_MARKUP, \"lxml\")\n",
      "\n",
      "  markup_type=markup_type))\n"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "更多关于你的信息\n",
      "更多关于你的信息\n",
      "更多关于你的信息\n",
      "更多关于你的信息\n",
      "更多关于你的信息\n",
      "更多关于你的信息\n",
      "更多关于你的信息\n",
      "更多关于你的信息\n",
      "更多关于你的信息\n",
      "更多关于你的信息\n",
      "更多关于你的信息\n",
      "更多关于你的信息\n",
      "更多关于你的信息\n",
      "更多关于你的信息\n"
     ]
    },
    {
     "ename": "KeyboardInterrupt",
     "evalue": "",
     "output_type": "error",
     "traceback": [
      "\u001b[1;31m---------------------------------------------------------------------------\u001b[0m",
      "\u001b[1;31mKeyboardInterrupt\u001b[0m                         Traceback (most recent call last)",
      "\u001b[1;32m<ipython-input-146-5f76c01d2726>\u001b[0m in \u001b[0;36m<module>\u001b[1;34m()\u001b[0m\n\u001b[1;32m----> 1\u001b[1;33m \u001b[0mfb\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mdownload_photos_one\u001b[0m\u001b[1;33m(\u001b[0m\u001b[1;34m\"https://www.facebook.com/profile.php?id=100025148614740\"\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m",
      "\u001b[1;32m<ipython-input-95-87bfb7c60b37>\u001b[0m in \u001b[0;36mdownload_photos_one\u001b[1;34m(self, _homepage_url)\u001b[0m\n\u001b[0;32m    279\u001b[0m \u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m    280\u001b[0m     \u001b[1;32mdef\u001b[0m \u001b[0mdownload_photos_one\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mself\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0m_homepage_url\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m:\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[1;32m--> 281\u001b[1;33m         \u001b[0mphotos_href_myself\u001b[0m \u001b[1;33m=\u001b[0m \u001b[0mself\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mget_photos_list\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0m_homepage_url\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m\u001b[0;32m    282\u001b[0m         \u001b[0mphotos_info_list\u001b[0m \u001b[1;33m=\u001b[0m \u001b[0mself\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mget_photos_info_list\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mphotos_href_myself\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m    283\u001b[0m         \u001b[1;32mfor\u001b[0m \u001b[0mphoto_info\u001b[0m \u001b[1;32min\u001b[0m \u001b[0mphotos_info_list\u001b[0m\u001b[1;33m:\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n",
      "\u001b[1;32m<ipython-input-95-87bfb7c60b37>\u001b[0m in \u001b[0;36mget_photos_list\u001b[1;34m(self, _homepage_url)\u001b[0m\n\u001b[0;32m    226\u001b[0m             \u001b[0mtime\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0msleep\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mtimeout\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m    227\u001b[0m \u001b[1;33m\u001b[0m\u001b[0m\n\u001b[1;32m--> 228\u001b[1;33m             \u001b[0mpage\u001b[0m \u001b[1;33m=\u001b[0m \u001b[0mself\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mdriver\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mpage_source\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m\u001b[0;32m    229\u001b[0m             \u001b[0msoup\u001b[0m \u001b[1;33m=\u001b[0m \u001b[0mBeautifulSoup\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mpage\u001b[0m\u001b[1;33m,\u001b[0m \u001b[1;34m\"html.parser\"\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m    230\u001b[0m             \u001b[0mbottom_flag\u001b[0m \u001b[1;33m=\u001b[0m \u001b[0msoup\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mfind_all\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mclass_\u001b[0m\u001b[1;33m=\u001b[0m\u001b[1;34m\"uiHeaderTitle\"\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n",
      "\u001b[1;32mc:\\program files\\python35\\lib\\site-packages\\selenium\\webdriver\\remote\\webdriver.py\u001b[0m in \u001b[0;36mpage_source\u001b[1;34m(self)\u001b[0m\n\u001b[0;32m    668\u001b[0m             \u001b[0mdriver\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mpage_source\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m    669\u001b[0m         \"\"\"\n\u001b[1;32m--> 670\u001b[1;33m         \u001b[1;32mreturn\u001b[0m \u001b[0mself\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mexecute\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mCommand\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mGET_PAGE_SOURCE\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m[\u001b[0m\u001b[1;34m'value'\u001b[0m\u001b[1;33m]\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m\u001b[0;32m    671\u001b[0m \u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m    672\u001b[0m     \u001b[1;32mdef\u001b[0m \u001b[0mclose\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mself\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m:\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n",
      "\u001b[1;32mc:\\program files\\python35\\lib\\site-packages\\selenium\\webdriver\\remote\\webdriver.py\u001b[0m in \u001b[0;36mexecute\u001b[1;34m(self, driver_command, params)\u001b[0m\n\u001b[0;32m    308\u001b[0m \u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m    309\u001b[0m         \u001b[0mparams\u001b[0m \u001b[1;33m=\u001b[0m \u001b[0mself\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0m_wrap_value\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mparams\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[1;32m--> 310\u001b[1;33m         \u001b[0mresponse\u001b[0m \u001b[1;33m=\u001b[0m \u001b[0mself\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mcommand_executor\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mexecute\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mdriver_command\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0mparams\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m\u001b[0;32m    311\u001b[0m         \u001b[1;32mif\u001b[0m \u001b[0mresponse\u001b[0m\u001b[1;33m:\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m    312\u001b[0m             \u001b[0mself\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0merror_handler\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mcheck_response\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mresponse\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n",
      "\u001b[1;32mc:\\program files\\python35\\lib\\site-packages\\selenium\\webdriver\\remote\\remote_connection.py\u001b[0m in \u001b[0;36mexecute\u001b[1;34m(self, command, params)\u001b[0m\n\u001b[0;32m    464\u001b[0m         \u001b[0mdata\u001b[0m \u001b[1;33m=\u001b[0m \u001b[0mutils\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mdump_json\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mparams\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m    465\u001b[0m         \u001b[0murl\u001b[0m \u001b[1;33m=\u001b[0m \u001b[1;34m'%s%s'\u001b[0m \u001b[1;33m%\u001b[0m \u001b[1;33m(\u001b[0m\u001b[0mself\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0m_url\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0mpath\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[1;32m--> 466\u001b[1;33m         \u001b[1;32mreturn\u001b[0m \u001b[0mself\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0m_request\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mcommand_info\u001b[0m\u001b[1;33m[\u001b[0m\u001b[1;36m0\u001b[0m\u001b[1;33m]\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0murl\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0mbody\u001b[0m\u001b[1;33m=\u001b[0m\u001b[0mdata\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m\u001b[0;32m    467\u001b[0m \u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m    468\u001b[0m     \u001b[1;32mdef\u001b[0m \u001b[0m_request\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mself\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0mmethod\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0murl\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0mbody\u001b[0m\u001b[1;33m=\u001b[0m\u001b[1;32mNone\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m:\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n",
      "\u001b[1;32mc:\\program files\\python35\\lib\\site-packages\\selenium\\webdriver\\remote\\remote_connection.py\u001b[0m in \u001b[0;36m_request\u001b[1;34m(self, method, url, body)\u001b[0m\n\u001b[0;32m    488\u001b[0m             \u001b[1;32mtry\u001b[0m\u001b[1;33m:\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m    489\u001b[0m                 \u001b[0mself\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0m_conn\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mrequest\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mmethod\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0mparsed_url\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mpath\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0mbody\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0mheaders\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[1;32m--> 490\u001b[1;33m                 \u001b[0mresp\u001b[0m \u001b[1;33m=\u001b[0m \u001b[0mself\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0m_conn\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mgetresponse\u001b[0m\u001b[1;33m(\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m\u001b[0;32m    491\u001b[0m             \u001b[1;32mexcept\u001b[0m \u001b[1;33m(\u001b[0m\u001b[0mhttplib\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mHTTPException\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0msocket\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0merror\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m:\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m    492\u001b[0m                 \u001b[0mself\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0m_conn\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mclose\u001b[0m\u001b[1;33m(\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n",
      "\u001b[1;32mc:\\program files\\python35\\lib\\http\\client.py\u001b[0m in \u001b[0;36mgetresponse\u001b[1;34m(self)\u001b[0m\n\u001b[0;32m   1196\u001b[0m         \u001b[1;32mtry\u001b[0m\u001b[1;33m:\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m   1197\u001b[0m             \u001b[1;32mtry\u001b[0m\u001b[1;33m:\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[1;32m-> 1198\u001b[1;33m                 \u001b[0mresponse\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mbegin\u001b[0m\u001b[1;33m(\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m\u001b[0;32m   1199\u001b[0m             \u001b[1;32mexcept\u001b[0m \u001b[0mConnectionError\u001b[0m\u001b[1;33m:\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m   1200\u001b[0m                 \u001b[0mself\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mclose\u001b[0m\u001b[1;33m(\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n",
      "\u001b[1;32mc:\\program files\\python35\\lib\\http\\client.py\u001b[0m in \u001b[0;36mbegin\u001b[1;34m(self)\u001b[0m\n\u001b[0;32m    295\u001b[0m         \u001b[1;31m# read until we get a non-100 response\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m    296\u001b[0m         \u001b[1;32mwhile\u001b[0m \u001b[1;32mTrue\u001b[0m\u001b[1;33m:\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[1;32m--> 297\u001b[1;33m             \u001b[0mversion\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0mstatus\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0mreason\u001b[0m \u001b[1;33m=\u001b[0m \u001b[0mself\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0m_read_status\u001b[0m\u001b[1;33m(\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m\u001b[0;32m    298\u001b[0m             \u001b[1;32mif\u001b[0m \u001b[0mstatus\u001b[0m \u001b[1;33m!=\u001b[0m \u001b[0mCONTINUE\u001b[0m\u001b[1;33m:\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m    299\u001b[0m                 \u001b[1;32mbreak\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n",
      "\u001b[1;32mc:\\program files\\python35\\lib\\http\\client.py\u001b[0m in \u001b[0;36m_read_status\u001b[1;34m(self)\u001b[0m\n\u001b[0;32m    256\u001b[0m \u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m    257\u001b[0m     \u001b[1;32mdef\u001b[0m \u001b[0m_read_status\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mself\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m:\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[1;32m--> 258\u001b[1;33m         \u001b[0mline\u001b[0m \u001b[1;33m=\u001b[0m \u001b[0mstr\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mself\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mfp\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mreadline\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0m_MAXLINE\u001b[0m \u001b[1;33m+\u001b[0m \u001b[1;36m1\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m,\u001b[0m \u001b[1;34m\"iso-8859-1\"\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m\u001b[0;32m    259\u001b[0m         \u001b[1;32mif\u001b[0m \u001b[0mlen\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mline\u001b[0m\u001b[1;33m)\u001b[0m \u001b[1;33m>\u001b[0m \u001b[0m_MAXLINE\u001b[0m\u001b[1;33m:\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m    260\u001b[0m             \u001b[1;32mraise\u001b[0m \u001b[0mLineTooLong\u001b[0m\u001b[1;33m(\u001b[0m\u001b[1;34m\"status line\"\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n",
      "\u001b[1;32mc:\\program files\\python35\\lib\\socket.py\u001b[0m in \u001b[0;36mreadinto\u001b[1;34m(self, b)\u001b[0m\n\u001b[0;32m    574\u001b[0m         \u001b[1;32mwhile\u001b[0m \u001b[1;32mTrue\u001b[0m\u001b[1;33m:\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m    575\u001b[0m             \u001b[1;32mtry\u001b[0m\u001b[1;33m:\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[1;32m--> 576\u001b[1;33m                 \u001b[1;32mreturn\u001b[0m \u001b[0mself\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0m_sock\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mrecv_into\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mb\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m\u001b[0;32m    577\u001b[0m             \u001b[1;32mexcept\u001b[0m \u001b[0mtimeout\u001b[0m\u001b[1;33m:\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m    578\u001b[0m                 \u001b[0mself\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0m_timeout_occurred\u001b[0m \u001b[1;33m=\u001b[0m \u001b[1;32mTrue\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n",
      "\u001b[1;31mKeyboardInterrupt\u001b[0m: "
     ]
    }
   ],
   "source": [
    "fb.download_photos_one(\"https://www.facebook.com/profile.php?id=100025148614740\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 168,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "https://www.facebook.com/erlyn.jumawan.7?fref=pb&hc_location=friends_tab\n",
      "Erlyn Jumawan\n",
      "100006804158012\n"
     ]
    }
   ],
   "source": [
    "# email = \"2432659496@qq.com\"\n",
    "# password = \"qianxuewu\"\n",
    "# fb = Facebook(email, password, \"Chrome\")\n",
    "# fb.log_in()\n",
    "url = \"https://www.facebook.com/profile.php?id=100025148614740&lst=100025148614740%3A100025148614740%3A1523031014&sk=friends&source_ref=pb_friends_tl\"\n",
    "# fb.driver.get(url)\n",
    "page = fb.driver.page_source\n",
    "soup = BeautifulSoup(page, \"html.parser\")\n",
    "content = soup.find(class_=\"uiProfileBlockContent\")\n",
    "print(content.a.get(\"href\"))\n",
    "print(content.a.text)\n",
    "pattern = re.compile(r\"id=\\d+\")\n",
    "_id = pattern.findall(content.a.get(\"data-hovercard\"))[0].split(\"id=\")[-1]\n",
    "print(_id)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 171,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Erlyn Jumawan 100006804158012 https://www.facebook.com/erlyn.jumawan.7\n",
      "Sudheer Kumar 100025029671192 https://www.facebook.com/profile.php?id=100025029671192\n",
      "Md Riayd 100023959088724 https://www.facebook.com/md.riayd.7140\n",
      "Nikhil Kumar 100022803730511 https://www.facebook.com/profile.php?id=100022803730511\n",
      "Tuyết Như Bùi 100023627592898 https://www.facebook.com/tuyetnhu.bui.902\n",
      "Nguyễn Ngọc Nhàn 100011745667270 https://www.facebook.com/nhanngocnguyen.nguyen\n",
      "Fero Collo 100024658776195 https://www.facebook.com/collo.fero\n",
      "Fain Sunar 100025176037330 https://www.facebook.com/fain.sunar\n",
      "Zahed Ahmed 100024292640418 https://www.facebook.com/zahed.ahmed.9619\n",
      "Don Masud Khan 100012959195477 https://www.facebook.com/donmasud.khan.9\n",
      "Tachya Santos 100024750171627 https://www.facebook.com/tachya.santos.94\n",
      "Akash Lakade 100024119840847 https://www.facebook.com/akash.lakade.3551\n",
      "MD Jumon MD Jumon 100023107142198 https://www.facebook.com/mdjumon.mdjumon.315\n",
      "খোখন মিয়া 100021706361596 https://www.facebook.com/profile.php?id=100021706361596\n",
      "Md Makh 100015098386797 https://www.facebook.com/md.makh.3\n",
      "KingLess Queen 100021582201117 https://www.facebook.com/profile.php?id=100021582201117\n",
      "Ch Ahmad Kahloon 100024816621101 https://www.facebook.com/chahmad.kahloon.7\n",
      "RAkib RAkib 100022127991791 https://www.facebook.com/profile.php?id=100022127991791\n",
      "Sohel Dhali 100019714574635 https://www.facebook.com/sohel.dhali.522\n",
      "ভোলার ছেলে নুর 100024513502434 https://www.facebook.com/profile.php?id=100024513502434\n",
      "Hajer Rs 100017166871539 https://www.facebook.com/hajer.rs.3\n",
      "Md. Alim Uddin 100025101336885 https://www.facebook.com/profile.php?id=100025101336885\n",
      "Jubayer Ahmed 100017409675153 https://www.facebook.com/jubayer.khar\n",
      "আমি তোমার হতে চাই 100017291017271 https://www.facebook.com/profile.php?id=100017291017271\n",
      "San Sameth 100022681714168 https://www.facebook.com/san.sameth.3\n",
      "Reymark Gemoya 100004363711310 https://www.facebook.com/roblereymark\n",
      "Nguyen Lai 100024082658977 https://www.facebook.com/profile.php?id=100024082658977\n",
      "Anowar Rana 100024796689411 https://www.facebook.com/anowar.rana.7587\n",
      "Andria Syifa 100024524415138 https://www.facebook.com/andria.syifa.3\n",
      "Syed Jikrul Islam 100010514881376 https://www.facebook.com/syed.jikrulislam.1\n",
      "Minh Minh 100024744552643 https://www.facebook.com/profile.php?id=100024744552643\n",
      "Md Parviz 100016276989962 https://www.facebook.com/md.parviz.750\n",
      "Rajesh Bansal 100024809323483 https://www.facebook.com/profile.php?id=100024809323483\n",
      "Hai Le 100022998261927 https://www.facebook.com/profile.php?id=100022998261927\n",
      "Nguyễn Minh Thư 100024348563827 https://www.facebook.com/profile.php?id=100024348563827\n",
      "Bijoy Bowmik 100024890711347 https://www.facebook.com/bijoy.bowmik.3\n",
      "Nur Hohain 100023639358016 https://www.facebook.com/nur.hohain\n",
      "Sk Khaja 100022861667554 https://www.facebook.com/sk.khaja.3979489\n",
      "Ba Lý 100013747338582 https://www.facebook.com/profile.php?id=100013747338582\n",
      "মিজান খান 100024556752408 https://www.facebook.com/profile.php?id=100024556752408\n",
      "Abdur Razzak Razzak 100024630242475 https://www.facebook.com/profile.php?id=100024630242475\n",
      "Md Tusar 100004810085317 https://www.facebook.com/profile.php?id=100004810085317\n",
      "JelBy L'v 100017428027402 https://www.facebook.com/jee.sila.5\n",
      "Imran Islam 100025052447500 https://www.facebook.com/imran.islam.77398143\n",
      "Ngoc Deo Phung Thi 100024361160951 https://www.facebook.com/ngocdeo.phungthi\n",
      "Nguyen Chi 100025015730355 https://www.facebook.com/profile.php?id=100025015730355\n",
      "राहुल चौहान राहुल चौहान 100024536663729 https://www.facebook.com/profile.php?id=100024536663729\n",
      "Sapana Gautam 100022745012675 https://www.facebook.com/sapana.gautam.7528\n",
      "Son Hoang 100025080464279 https://www.facebook.com/profile.php?id=100025080464279\n",
      "Jahangeer Alam 100021920562130 https://www.facebook.com/jahangeer.alam.904750\n",
      "Md Suhidulla 100024771094769 https://www.facebook.com/md.suhidulla.92\n",
      "মো সোহাগ হোসেন 100024705887203 https://www.facebook.com/profile.php?id=100024705887203\n",
      "Md Shabash 100023902242861 https://www.facebook.com/md.shabash.5\n",
      "MD Alom 100022090727253 https://www.facebook.com/profile.php?id=100022090727253\n",
      "Musafir Mah 100021899110275 https://www.facebook.com/musafir.mah.9\n",
      "রাসেল তিনভাইয়ের দোকান 100024900338141 https://www.facebook.com/profile.php?id=100024900338141\n",
      "Chan Tha 100023852941971 https://www.facebook.com/profile.php?id=100023852941971\n",
      "Md Aziz Khan 100021712121225 https://www.facebook.com/mdaziz.khan.5832343\n",
      "Vuphuong Duong 100024868896316 https://www.facebook.com/vuphuong.duong.123276\n",
      "Kausar Ahmed 100024555605754 https://www.facebook.com/profile.php?id=100024555605754\n",
      "অচেনা একটি পাখী 100023234525574 https://www.facebook.com/profile.php?id=100023234525574\n",
      "Antnio Ramirez 100025100891604 https://www.facebook.com/antnio.ramirez.925\n",
      "Md Sohel 100013403820779 https://www.facebook.com/profile.php?id=100013403820779\n",
      "Mahedi Hassan 100023299952982 https://www.facebook.com/profile.php?id=100023299952982\n",
      "Vu Hai Nguyen 100025069604917 https://www.facebook.com/vuhai.nguyen.3979\n",
      "Sakaeid Sobis 100024414832841 https://www.facebook.com/sakaeid.sobis\n",
      "Bipin Magar 100023973973340 https://www.facebook.com/bipin.magar.79656\n",
      "Shakeel Salamat 100023163272940 https://www.facebook.com/shakeel.salamat.31\n",
      "Arifkhan Arifkhan 100015630848126 https://www.facebook.com/profile.php?id=100015630848126\n",
      "একা কেন সাকিব 100025140635815 https://www.facebook.com/profile.php?id=100025140635815\n",
      "Md Forhad 100014121132564 https://www.facebook.com/profile.php?id=100014121132564\n",
      "Md Easin 100025088770763 https://www.facebook.com/profile.php?id=100025088770763\n",
      "Nirakar Biswal 100004691621481 https://www.facebook.com/nirakar.biswal.18\n",
      "Noy Olga 100024546378001 https://www.facebook.com/noy.olga.5\n",
      "MD Nurislam MD Nurislam 100024457689491 https://www.facebook.com/mdnurislam.mdnurislam.3950\n",
      "আরাফাত আরাফাত 100024772230270 https://www.facebook.com/akish.akish.925059\n",
      "Jamal Khan 100024770280854 https://www.facebook.com/profile.php?id=100024770280854\n",
      "Mahmudul Hossen 100015467623846 https://www.facebook.com/mahmudul.hassion\n",
      "Jose Lapara Alexade 100017089595353 https://www.facebook.com/joselapara.alexade\n",
      "Md Ashiq 100016565461171 https://www.facebook.com/mdaridhosan.hosan\n",
      "Aarsh Aarsh 100021525672662 https://www.facebook.com/aarsh.aarsh.9440234\n",
      "Misba Laskar 100024486021386 https://www.facebook.com/misba.laskar.923\n",
      "Ridoy Ahmed 100018034783946 https://www.facebook.com/profile.php?id=100018034783946\n",
      "Kiệt Trai Ngoan 100020154497057 https://www.facebook.com/traingoan.kiet\n",
      "Tít's Quậy's 100023938112149 https://www.facebook.com/quays.tits.98\n",
      "Md Mahabub 100024694254019 https://www.facebook.com/profile.php?id=100024694254019\n",
      "Daud King 100021982218508 https://www.facebook.com/profile.php?id=100021982218508\n",
      "Harun Harun 100024727131273 https://www.facebook.com/profile.php?id=100024727131273\n",
      "Ruman Ahmed 100022595916696 https://www.facebook.com/ruman.ahmedpaglurobin\n",
      "Md Jamal Hossain 100013166502026 https://www.facebook.com/profile.php?id=100013166502026\n",
      "Nguyễn Sang 100023939827614 https://www.facebook.com/profile.php?id=100023939827614\n",
      "Mai Ngân 100025063878222 https://www.facebook.com/mai.ngan.566790\n",
      "Nguyễn Ngọc 100024595243587 https://www.facebook.com/profile.php?id=100024595243587\n",
      "Bina Akther 100006118427873 https://www.facebook.com/profile.php?id=100006118427873\n",
      "Md Musa 100022813680506 https://www.facebook.com/profile.php?id=100022813680506\n",
      "Ngọc Lò 100022546932595 https://www.facebook.com/ngoc.lo.940641\n",
      "Vitya Markov 100014271963108 https://www.facebook.com/profile.php?id=100014271963108\n"
     ]
    },
    {
     "ename": "IndexError",
     "evalue": "list index out of range",
     "output_type": "error",
     "traceback": [
      "\u001b[1;31m---------------------------------------------------------------------------\u001b[0m",
      "\u001b[1;31mIndexError\u001b[0m                                Traceback (most recent call last)",
      "\u001b[1;32m<ipython-input-171-e84325d7703c>\u001b[0m in \u001b[0;36m<module>\u001b[1;34m()\u001b[0m\n\u001b[0;32m     27\u001b[0m \u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m     28\u001b[0m \u001b[1;32mfor\u001b[0m \u001b[0mi\u001b[0m \u001b[1;32min\u001b[0m \u001b[0mrange\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mfriends_number\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m:\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[1;32m---> 29\u001b[1;33m     \u001b[0mprint\u001b[0m\u001b[1;33m(\u001b[0m\u001b[1;34m\"%s %s %s\"\u001b[0m \u001b[1;33m%\u001b[0m \u001b[1;33m(\u001b[0m\u001b[0muser_info_friends\u001b[0m\u001b[1;33m[\u001b[0m\u001b[0mi\u001b[0m\u001b[1;33m]\u001b[0m\u001b[1;33m[\u001b[0m\u001b[1;36m0\u001b[0m\u001b[1;33m]\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0muser_info_friends\u001b[0m\u001b[1;33m[\u001b[0m\u001b[0mi\u001b[0m\u001b[1;33m]\u001b[0m\u001b[1;33m[\u001b[0m\u001b[1;36m1\u001b[0m\u001b[1;33m]\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0muser_info_friends\u001b[0m\u001b[1;33m[\u001b[0m\u001b[0mi\u001b[0m\u001b[1;33m]\u001b[0m\u001b[1;33m[\u001b[0m\u001b[1;36m2\u001b[0m\u001b[1;33m]\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m",
      "\u001b[1;31mIndexError\u001b[0m: list index out of range"
     ]
    }
   ],
   "source": [
    "# friends_table = soup.find(class_=fb.friends_class_name)              # class name可能会变\n",
    "# content = friends_table.a.text\n",
    "# pattern = re.compile(r\"\\d+\\.?\\d*\")\n",
    "# friends_number = int(pattern.findall(content)[0])\n",
    "\n",
    "# # 根据好友数量进行页面刷新\n",
    "# refresh_times = friends_number // 20\n",
    "# fb.page_refresh(refresh_times)\n",
    "\n",
    "# # 重新对刷新后的页面进行解析\n",
    "# page_source = fb.driver.page_source\n",
    "# soup = BeautifulSoup(page_source, \"html.parser\")\n",
    "\n",
    "# # 获取好友url列表\n",
    "# contents = soup.find_all(class_=\"uiProfileBlockContent\")\n",
    "# user_info_friends = list()\n",
    "# for content in contents:\n",
    "#     homepage_url = content.a.get(\"href\")\n",
    "#     if url_type_judge(homepage_url) == 1:\n",
    "#         homepage_url = homepage_url.replace(\"?fref=pb&hc_location=friends_tab\", \"\")\n",
    "#     if url_type_judge(homepage_url) == 2:\n",
    "#         homepage_url = homepage_url.replace(\"&fref=pb&hc_location=friends_tab\", \"\")\n",
    "#     user_name = content.a.text\n",
    "#     pattern = re.compile(r\"id=\\d+\")\n",
    "#     user_id = pattern.findall(content.a.get(\"data-hovercard\"))[0].split(\"id=\")[-1]\n",
    "#     user_info_friends.append([user_name, user_id, homepage_url])\n",
    "\n",
    "print(\"User Name\", \"User ID\", \"Homepage URL\")\n",
    "for i in range(friends_number):\n",
    "    print(\"%s %s %s\" % (user_info_friends[i][0], user_info_friends[i][1], user_info_friends[i][2]))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.5.3"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
